<?php
	$GLOBALS['THRIFT_ROOT'] = '/usr/share/php/thrift/';
	require_once $GLOBALS['THRIFT_ROOT'] . 'packages/cassandra/Cassandra.php';
	require_once $GLOBALS['THRIFT_ROOT'] . 'packages/cassandra/cassandra_types.php';
	require_once $GLOBALS['THRIFT_ROOT'] . 'transport/TSocket.php';
	require_once $GLOBALS['THRIFT_ROOT'] . 'protocol/TBinaryProtocol.php';
	require_once $GLOBALS['THRIFT_ROOT'] . 'transport/TFramedTransport.php';
	require_once $GLOBALS['THRIFT_ROOT'] . 'transport/TBufferedTransport.php';
	
	class Webstar_Cassandra_Db {
		
		private $socket;
		private $transport;
		private $protocol;
		
		private $db_client;
		
		public function __construct(){
			try{
				
	//			TODO host should be dynamic	
				$this->socket = new TSocket('127.0.0.1', 9160);
				
				$this->transport = new TBufferedTransport($this->socket, 1024, 1024);
				
				$this->protocol = new TBinaryProtocolAccelerated($this->transport);
				
				$this->db_client = new CassandraClient($this->protocol);
				
				
			}catch (TException $tx){
				
				print 'TException: '.$tx->why. ' Error: '.$tx->getMessage() . "\n";
				
			}
		}
		
		public function get_keyspaces(){
			$this->transport->open();
			$keyspaces = $this->db_client->describe_keyspaces();
			$this->transport->close(); 
			return 	$keyspaces;
		}
		
		public function get_keyspace($name){
			$this->transport->open();
			$keyspace = $this->db_client->describe_keyspace($name); 
			$this->transport->close();
			return $keyspace;
		}
		
	}